﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASPItalia.ModelVirtualCasting.WPF.Common;
using System.ComponentModel;
using System.Windows.Data;
using ASPItalia.ModelVirtualCasting.WPF.Models;
using Proxy = ASPItalia.ModelVirtualCasting.WPF.ViewModels.Proxy;

namespace ASPItalia.ModelVirtualCasting.WPF.ViewModels
{
    public class NewCandidatesViewModel : AsyncViewModel
    {

        private ObservableCollectionEx<ModelViewModel> _candidates;

        public ObservableCollectionEx<ModelViewModel> Candidates
        {
            get
            {
                if (_candidates == null)
                    _candidates = new ObservableCollectionEx<ModelViewModel>();
                return _candidates;
            }
        }

        public ICollectionView CandidatesView
        {
            get { return CollectionViewSource.GetDefaultView(this.Candidates); }
        }

        public void LoadAsync()
        {
            this.ExecuteOperationAsync(this.Load);
        }

        private void Load(AsyncOperation operation)
        {
            Proxy.ModelsManagerClient client = new Proxy.ModelsManagerClient();
            Proxy.GetAllCandidatesResponse response = client.GetAllCandidates(new Proxy.GetAllCandidatesRequest());

            operation.Post(s => this.Candidates.AddRange(response.Models.Select(m => GetModel(m))), null);

            // TEMP
            //operation.Post(s => this.Candidates.AddRange(Enumerable.Range(1, 10).Select(n => new ModelViewModel
            //{
            //    FullName = "Candidatura " + n,
            //    HairColor = "Castani",
            //    EyeColor = "Azzurri",
            //    IsCandidate = true,
            //    Multimedia =
            //    {
            //        new DesignMultimediaImage { UriSource = new Uri("pack://application:,,,/SampleData/Images/FakeModel (" + n + ").jpg")},
            //        new DesignMultimediaImage { UriSource = new Uri("pack://application:,,,/SampleData/Images/FakeModel (" + n + ").jpg")},
            //        new DesignMultimediaMovie { VideoUri = new Uri("http://www.test.it") },
            //    }
            //})), null);
        }

        private ModelViewModel GetModel(Proxy.Model m)
        {
            ModelViewModel mvm = new ModelViewModel
            {
                EyeColor = m.EyeColor,
                HairColor = m.HairColor,
                IsCandidate = true,
                FullName = m.FirstName + " " + m.LastName,
                CanBeNaked = true,
            };

            mvm.Multimedia.AddRange(m.Multimedia.Select(mu => GetMultimediaModel(mu)));

            return mvm;
        }

        private ASPItalia.ModelVirtualCasting.WPF.Models.Multimedia GetMultimediaModel(Proxy.Multimedia mu)
        {
            if (mu is Proxy.Image)
                return new DesignMultimediaImage { UriSource = new Uri("http://localhost:39956/Images/" + mu.FileName) };
            else
                return new DesignMultimediaMovie { VideoUri = new Uri("http://localhost:39956/Video/" + mu.FileName) };
        }


    }
}
